package com.jane.gzzb;

import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.jane.adapter.MyFragmentPagerAdapter;
import com.jane.fragment.homepagefragment.MeAttentionFragment;
import com.jane.fragment.homepagefragment.SeorinityFragment;
import com.jane.gzzb.R;

import java.util.ArrayList;
import java.util.List;

public class GengDuoActivity extends AppCompatActivity implements View.OnClickListener, ViewPager.OnPageChangeListener, RadioGroup.OnCheckedChangeListener {
    //申明
    private TextView mBack;
    private ViewPager mViewPager;
    private List<Fragment> mFragments;
    private MyFragmentPagerAdapter mAdapter;
    private RadioGroup mRadioGroup;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_geng_duo);
        initView();
        initData();
        initListener();
    }

    private void initData() {
        mFragments = new ArrayList<>();
        MeAttentionFragment meFragment = new MeAttentionFragment();
        mFragments.add(meFragment);
        SeorinityFragment seFragment = new SeorinityFragment();
        mFragments.add(seFragment);
        //设置适配器
        mAdapter = new MyFragmentPagerAdapter(getSupportFragmentManager(), mFragments);
        mViewPager.setAdapter(mAdapter);
    }

    private void initListener() {
        mBack.setOnClickListener(this);
        mViewPager.addOnPageChangeListener(this);
        mRadioGroup.setOnCheckedChangeListener(this);
    }

    private void initView() {
        mBack = (TextView) findViewById(R.id.activity_gengduo_return);
        mViewPager = (ViewPager) findViewById(R.id.activity_gengduo_viewpager);
        mRadioGroup = (RadioGroup) findViewById(R.id.activity_gengduo_radiogroup);
        ((RadioButton) mRadioGroup.getChildAt(0)).setChecked(true);//设置默认值
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.activity_gengduo_return:
                this.finish();
                break;
        }
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        RadioButton button = (RadioButton) mRadioGroup.getChildAt(position);
        button.setChecked(true);
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        int currentIndex = 0;
        switch (checkedId) {
            case R.id.activity_gengduo_meattention:
                currentIndex = 0;
                break;
            case R.id.activity_gengduo_seorinity:
                currentIndex = 1;
                break;
            default:
                break;
        }
        mViewPager.setCurrentItem(currentIndex);
    }
}
